package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SpringLayout;

import model.Role;
import model.User;
import controller.Controller;
import javax.swing.SwingConstants;
/**
 * Shows the details of a single Volunteer.
 * 
 * @author James Gunter - jpgunter
 *
 */
public class VolunteerView extends JPanel implements ActionListener{
  
  private static final int PREFERRED_WIDTH = 300;
  private static final int PREFERRED_HEIGHT = 300;
  private JButton my_back_button;
  private JButton my_home_button;
  private MainWindow my_parent;
  private User my_volunteer;
  private JButton my_blackball_button;

  public VolunteerView(MainWindow the_parent, User the_volunteer){
    setPreferredSize(new Dimension(PREFERRED_WIDTH, PREFERRED_HEIGHT));
    
    my_parent = the_parent;
    my_volunteer = the_volunteer;
    
    setLayout(new BorderLayout());
    
    JPanel header_panel = new JPanel(new BorderLayout());
    JLabel lblVolunteer = new JLabel("Volunteer");
    lblVolunteer.setHorizontalAlignment(SwingConstants.CENTER);
    header_panel.add(lblVolunteer, BorderLayout.CENTER);
    
    my_back_button = new JButton("Back");
    my_back_button.addActionListener(this);
    header_panel.add(my_back_button, BorderLayout.WEST);
    
    my_home_button = new JButton("Home");
    my_home_button.addActionListener(this);
    header_panel.add(my_home_button, BorderLayout.EAST);
    
    add(header_panel, BorderLayout.NORTH);
    
    SpringLayout sl_user_info_panel = new SpringLayout();
    JPanel user_info_panel = new JPanel(sl_user_info_panel);
    
    
    JLabel lblName = new JLabel("Name:");
    sl_user_info_panel.putConstraint(SpringLayout.NORTH, lblName, 10, SpringLayout.NORTH, user_info_panel);
    sl_user_info_panel.putConstraint(SpringLayout.WEST, lblName, 10, SpringLayout.WEST, user_info_panel);
    user_info_panel.add(lblName);
    
    JLabel lblJohnDoe = new JLabel(my_volunteer.getUserName());
    sl_user_info_panel.putConstraint(SpringLayout.NORTH, lblJohnDoe, 0, SpringLayout.NORTH, lblName);
    sl_user_info_panel.putConstraint(SpringLayout.WEST, lblJohnDoe, 6, SpringLayout.EAST, lblName);
    user_info_panel.add(lblJohnDoe);
    
    JLabel lblEmail = new JLabel("E-mail:");
    sl_user_info_panel.putConstraint(SpringLayout.NORTH, lblEmail, 6, SpringLayout.SOUTH, lblName);
    sl_user_info_panel.putConstraint(SpringLayout.WEST, lblEmail, 0, SpringLayout.WEST, lblName);
    user_info_panel.add(lblEmail);
    
    JLabel lblJohndmissingcom = new JLabel(my_volunteer.getUserEmail());
    sl_user_info_panel.putConstraint(SpringLayout.NORTH, lblJohndmissingcom, 0, SpringLayout.NORTH, lblEmail);
    sl_user_info_panel.putConstraint(SpringLayout.WEST, lblJohndmissingcom, 6, SpringLayout.EAST, lblEmail);
    user_info_panel.add(lblJohndmissingcom);
    
    if(my_volunteer.getUserRole() != Role.BLACKBALLED){
      my_blackball_button = new JButton("Blackball");
      my_blackball_button.addActionListener(this);
      sl_user_info_panel.putConstraint(SpringLayout.NORTH, my_blackball_button, 6, SpringLayout.SOUTH, lblEmail);
      sl_user_info_panel.putConstraint(SpringLayout.WEST, my_blackball_button, 0, SpringLayout.WEST, lblName);
      user_info_panel.add(my_blackball_button);
    }
    add(user_info_panel, BorderLayout.CENTER);
  }
  
  @Override
  public void actionPerformed(ActionEvent e) {
    if(e.getSource() == my_back_button){
      my_parent.back();
    } else if (e.getSource() == my_home_button){
      my_parent.home();
    } else if (e.getSource() == my_blackball_button){
      int result = JOptionPane.showOptionDialog(this, "Are you sure you want to blackball this user?", "Blackball Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null);
      if(result == JOptionPane.YES_OPTION){
        my_volunteer.setUserRole(Role.BLACKBALLED);
        my_parent.back();
      } 
    }
  }
}
